#encoding:utf-8
from __future__ import unicode_literals
from django.shortcuts import render,HttpResponse,HttpResponseRedirect,render_to_response
from ceshi import models
# Create your views here.
from ceshi.models import User
from django import forms
#注册表单
class UserForm(forms.Form):
    username = forms.CharField(label="用户",max_length=32)
    password = forms.CharField(label="用户",widget = forms.PasswordInput)
    email = forms.EmailField(label="邮箱")
    phone = forms.CharField(label="电话")
#登录表单
class LoginForm(forms.Form):
    username = forms.CharField(label="用户", max_length=32)
    password = forms.CharField(label="用户", widget=forms.PasswordInput)
#修改密码
class ChangeForm(forms.Form):
    sername = forms.CharField(label="用户",max_length=32)
    password = forms.CharField(label="用户", widget=forms.PasswordInput)
def regist(request):
    if request.method == 'post':
        uf = UserForm(request.post)
        if uf.is_valid:
            username = uf.cleaned_data['username']
            password = uf.cleaned_data['password']
            email = uf.cleaned_data['email']
            phone = uf.cleaned_data['phone']
            User.object.create(username = username,password = password,phone = phone,email =email)
            return HttpResponse('注册成功')
        else:
            uf = UserForm()
            return render_to_response('regist.html',{'uf':uf})

def login(request):
  #  if request.method == 'post':
  #     uf = LoginForm()
  #      if uf.is_valid():
  #          username = uf.cleaned_data['username']
  #          password = uf.cleaned_data['passsword']
 #           user=User.object.filter(username_exact = username,password_exact = password)
 #           if user:
#                reponse = HttpResponseRedirect('/login/index')
 #               return reponse
  #          else:
  #              return HttpResponseRedirect('/login/login')
 #       else:
 #           uf =LoginForm()
            return render_to_response('login.html',)

def index(request):
    return render_to_response('index.html')

def Changepass(request):
    if request.method == 'post':
        uf = Changepass(request.post)
        if uf.is_valid:
            username = uf.cleaned_data['username']
            password = uf.cleaned_data['password']
            user = User.object.filter('username = username').update('password = password')
            return HttpResponse('修改密码成功')
        else:
            uf = Changepass()
            return render_to_response('change.html',{'uf':uf})